<?php
namespace Admin\Controller;

class NavController extends AdminController 
{
    public function index($pid='0')
    {
        $model = M("Nav");
        
        $where = array(
            'status'    => '1',
            'pid'       => $pid,
        );
        // 查询满足要求的总记录数
        $count      = $model->where($where)->count();

        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($where)->order('sort ASC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
        
    }
    
    public function add($pid='0')
    {
        if(IS_POST)
        {
            $data = array(
                'name'  => I('post.name'),
                'sort'  => (int)I('post.sort'),
                'url'   => I('post.url'),
                'pid'   => (int)$pid,
                'status'    => '1',
                'create_time'   => time(),
            );
            $model = M("Nav");
            $id = $model->add($data);
            if(empty($id))
            {
                $this->error('新增失败');
            } else {
                $this->success("新增成功",U('index',array('pid'=>$pid)));
            }
        } else {
            $this->display();
        }
    }
    
    public function edit($id)
    {
        $model = M("Nav");
        if(IS_POST)
        {
            $data = array(
                'name'  => I('post.name'),
                'sort'  => (int)I('post.sort'),
                'url'   => I('post.url'),
                'update_time' => time(),
            );
            if($model->where(array('id'=>$id))->save($data))
            {
                $this->success('修改成功', U('index'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $info = $model->where(array('id'=>$id))->find();
            $this->assign('info', $info);
            $this->display('add');
        }
    }
    
    public function del($id)
    {
        $model = M("nav");
        if($model->where(array('id'=>$id))->delete())
        {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
